<?php

include_once 'Database/Connection.php';
include_once 'Language/Selector.php';

class ModelCreator_GetObjects {

    private $_allObjects;
    private $_language;

    public function __construct($arrPost) {
        $this->_path = $arrPost[path];
        $this->_language = $arrPost[language];
        unset($arrPost[language]);
        $this->_allObjects = new ModelCreator_Database_Connection($arrPost);
        $test = $this->_allObjects->objConn->_test;
        $privileges = $this->_allObjects->objConn->_privileges;
        if ($test) {
            $return['type'] = true;
            $return['msg'] = $this->mountXHTMLSuccess();
        } else {
            $return['type'] = false;
            $return['msg'] = $this->mountXHTMLError($test, $privileges);
        }
        echo json_encode($return);
    }

    public function mountXHTMLSuccess() {
        $lang = new ModelCreator_Language_Selector();
        $arrLanguage = $lang->detect();

        $tables = $this->_allObjects->objConn->_allTables;
        $i = 1;
        $xhtml = "<form id='frmTables'>";
        $xhtml .= "<table width='100%' cellspacing='2' id='selectTables'>";
        $xhtml .= "<tr colspan='5'>";
        $xhtml .= "<td  class='tdtable'>";
        $xhtml .= "<input type='checkbox' name='object[allTables]' value='ok' id='allTables' checked />{$arrLanguage['all-files']}";
        $xhtml .= "<input type='checkbox' name='noneTables' id='noneTables'/>{$arrLanguage['none-files']}</td><tr>";
        foreach ($tables as $value) {
            if ($i == 1) {
                $xhtml .= "<tr>";
            }
            $xhtml .= "<td width='20%' class='tdtable'>";
            $xhtml .= "<label>";
            $xhtml .= "<input type='checkbox' name='object[{$value}]' value='{$value}' checked />";
            $xhtml .= $value;
            $xhtml .= "</label>";
            $xhtml .= "</td>";
            if ($i == 5) {
                $xhtml .= "</tr>";
                $i = 1;
                continue;
            }
            $i++;
        }
        $xhtml .= "</table>";
        $xhtml .= "</form>";
        $xhtml .= "<div id='idResults'></div>";
        return $xhtml;
    }

    public function mountXHTMLError($test, $privileges) {
        $lang = new ModelCreator_Language_Selector();
        $arrLanguage = $lang->detect();
        $msg = (!$test) ? $arrLanguage['alert_msg'] : $arrLanguage['alert_privilege'];
        $xhtml = "<div class='ui-widget'>";
        $xhtml .= "<div class='ui-state-error ui-corner-all' style='padding: 0 .7em;'>";
        $xhtml .= "<p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>";
        $xhtml .= "<strong>{$arrLanguage['alert']}:</strong> {$msg}.</p></div></div>";
        return $xhtml;
    }

}